# coding:utf-8
import csv
import json

from loguru import logger

from getScript import CreatUser, logging



class Operation(CreatUser):

    # """获取所有 可测性api"""
    @logger.catch
    @logging("获取所有 可测性api")
    def getResources(self):
        url = 'http://xuat.wxp.woa.com/apis/v1/featurebaseline/findTestabilityApiList'
        data = {"pageSize":1000,"pageNum":1}

        re = self.post_uat(url, json.dumps(data))
        data = re['data']['testabilityApiList']
        self.dataList = [{'testabilityApiName': '可测性api名称'," ":" ", 'testabilityApiUrl': '接口Url', 'testabilityFeature': '特征', 'ApiUrl': '链接'}]
        for i in data:
            dataDict = {}
            if i['stateId'] == 32:
                dataDict['testabilityApiName'] = i['testabilityApiName']
                dataDict['zero'] = "   "
                dataDict['testabilityApiUrl'] = i['testabilityApiUrl']
                dataDict['testabilityFeature'] = i['testabilityFeature']
                dataDict['ApiUrl'] = 'http://xuat.wxp.woa.com/assistant/testabilityApiCompile/' + str(i['testabilityApiId'])
                self.dataList.append(dataDict)


    # 保存 可测性api数据
    @logger.catch
    @logging('保存 可测性api数据')
    def get_csv(self):
        with open("testabilityApi.csv", "w", newline="", encoding="utf8") as csvFile:
            csvFileWriter = csv.writer(csvFile)
            for data in self.dataList:
                print(data)
                csvFileWriter.writerow(data.values())


if __name__ == '__main__':
    # 获取所有生效的可测性api
    op = Operation()
    op.getResources()
    op.get_csv()
